package com.csd.action.document;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.InputStream;

import java.net.URLEncoder;
import com.csd.helper.ActionResult;
import com.csd.helper.Tool;

/**
 * 
 * Action:下载工程文档
 * 
 * @author csd
 * @version 1.0
 * 
 * <br>
 *          修订时间：2010-1-9 <br>
 *          修改原因：创建文件 <br>
 *          修改人：尹华彬 <br>
 * 
 */
public class FileDownloadAction {
	/** 初始的通过param指定的文件名属性 */
	private String fileName;
	/** 文档路径 */
	private String path;
	@SuppressWarnings("unused")
	private InputStream inputStream;

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getFileName() {
		return fileName;
	}

	public InputStream getInputStream() throws Exception {


		try {
			return new FileInputStream(Tool.getPath() + path);
		} catch (FileNotFoundException e) {

			// HttpServletResponse response =
			// ServletActionContext.getResponse();
			// response.sendRedirect(Tool.getPath()+"error.jsp");
			return new FileInputStream(Tool.getPath() + "userfile"
					+ File.separator + "err.html");
		}

	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String execute() throws Exception {

		return ActionResult.success;

	}

	public void setFileName(String fileName) {

		this.fileName = fileName;

	}

	/**
	 * 提供转换编码后的供下载用的文件名
	 * 
	 * @throws Exception
	 */

	public String getDownloadFileName() throws Exception {
		String downFileName = null;
		if (path.startsWith("userfile")) {

			int index = path.lastIndexOf(File.separator);
			fileName = path.substring(index + 1, path.length());
			downFileName = fileName;
			downFileName = URLEncoder.encode(downFileName, "utf-8");
		}

		System.out.println(downFileName);
		return downFileName;

	}
}
